package com.wzcl.app.controller;

import com.wzcl.app.dao.model.AppUser;
import com.wzcl.app.model.common.BusinessException;
import com.wzcl.app.model.common.CodeMsg;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.UserDto;
import com.wzcl.app.model.vo.AppUserVo;
import com.wzcl.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/user")
@Api(value = "用户管理controller", tags = {"用户管理相关接口"})
public class UserController {
    @Resource
    private UserService service;
    @Resource
    private RedisTokenStore redisTokenStore;

    @PostMapping("/add")
    @ApiOperation("管理员添加用户")
    public RVO<AppUserVo> addUser(@RequestBody @Valid UserDto userDto){
        AppUser user = service.addUser(userDto);
        return RVO.success(AppUserVo.create(user));
    }

    @ApiOperation("注销账号")
    @PostMapping("/del")
    public RVO<String> delUser(HttpServletRequest request){
        String header = request.getHeader("Authorization");
        if (header == null || !header.startsWith("Bearer")) {
            throw new UnapprovedClientAuthenticationException("请求头中无token信息");
        }
        String token = header.substring(7);
        OAuth2AccessToken oAuth2AccessToken = redisTokenStore.readAccessToken(token);
        if(oAuth2AccessToken == null){
            throw new BusinessException(CodeMsg.SYSTEM_ERR,"非法请求");
        }
        String phone = SecurityContextHolder.getContext().getAuthentication().getName();
        service.delUserByPhone(phone);
        //注销之后退出登录
        redisTokenStore.removeAccessToken(oAuth2AccessToken);
        return RVO.success("成功注销");
    }


    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    public RVO<AppUserVo> updateUser(@RequestBody @Valid UserDto userDto){
        AppUser user = service.updateUser(userDto);
        return RVO.success(AppUserVo.create(user));
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/updateAdmin")
    public RVO<AppUserVo> updateAdminUser(@RequestBody @Valid UserDto userDto){
        AppUser user = service.updateAdminUser(userDto);
        return RVO.success(AppUserVo.create(user));
    }

    @PostMapping("/get")
    @ApiOperation("获取用户信息")
    public RVO<AppUserVo> getUser(@RequestBody UserDto userDto){
        AppUser user = service.getUserById(userDto.getId());
        if (user!=null){
            return RVO.success(AppUserVo.create(user));
        }
        return RVO.success(null);
    }

    @PostMapping("/getMy")
    @ApiOperation("获取用户信息")
    public RVO<AppUserVo> getUser(){
        String userPhone = SecurityContextHolder.getContext().getAuthentication().getName();
        AppUser user = service.getUserByPhone(userPhone);
        if (user!=null){
            return RVO.success(AppUserVo.create(user));
        }
        return RVO.success(null);
    }


    @ApiOperation("获取用户信息List")
    @PostMapping("/getList")
    public RVO<List<AppUserVo>> getUserList(@RequestBody UserDto userDto){
        List<AppUser> list = service.getUserList(userDto);
        if(list != null && list.size() >0){
            List<AppUserVo> collect = list.stream().map(AppUserVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("获取用户信息List")
    @PostMapping("/getPage")
    public RVO<List<AppUserVo>> getUserPage(@RequestBody UserDto userDto){
        MyPage<AppUser> page = service.getUserPage(userDto);
        if(page != null && page.getContent().size()>0){
            List<AppUserVo> collect = page.getContent().stream().map(AppUserVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return RVO.success(null);
    }

    @ApiOperation("修改用户密码")
    @PostMapping("/editPwd")
    public RVO<AppUserVo> editPwd(@RequestBody UserDto userDto){
        AppUser user = service.editPwd(userDto);
        return RVO.success(AppUserVo.create(user));
    }

    @ApiOperation("修改用户状态")
    @PostMapping("/changeUserState")
    public RVO<AppUserVo> changeUserState(@RequestBody UserDto userDto){
        AppUser user = service.changeUserState(userDto);
        return RVO.success(AppUserVo.create(user));
    }

    @ApiOperation("修改用户年级")
    @PostMapping("/changeUserGrade")
    public RVO<AppUserVo> changeUserGrade(@RequestBody UserDto userDto){
        AppUser user = service.changeUserGrade(userDto);
        return RVO.success(AppUserVo.create(user));
    }

    @ApiOperation("修改用户真实姓名")
    @PostMapping("/changeRealName")
    public RVO<AppUserVo> changeRealName(@RequestBody UserDto userDto){
        AppUser user = service.changeRealName(userDto);
        return RVO.success(AppUserVo.create(user));
    }

}
